﻿using System.Drawing;
using System.Drawing.Imaging;

namespace Netkk.Library
{
  public  class ImageHandling
    {
        public static Bitmap ResizeOrCropImage(Bitmap bitmap, int width, int height)
        {
            Image tmpImage = (Image)bitmap;
            // Crop
            if (width > 0 && height > 0)
                tmpImage = Crop(tmpImage, width, height, AnchorPosition.Center);

            // Resize
            if (width > 0 && width < bitmap.Width)
                tmpImage = ConstrainProportions(tmpImage, width, Dimensions.Width);
            else if (height > 0 && height < bitmap.Height)
                tmpImage = ConstrainProportions(tmpImage, height, Dimensions.Height);

            return (Bitmap)tmpImage;
        }

        public static Image Crop(Image imgPhoto, int Width, int Height, AnchorPosition Anchor)
        {
            int sourceWidth = imgPhoto.Width;
            int sourceHeight = imgPhoto.Height;
            int sourceX = 0;
            int sourceY = 0;
            int destX = -1;
            int destY = -1;

            double nPercent = 0;
            double nPercentW = 0;
            double nPercentH = 0;


            nPercentW = ((double)Width / (double)sourceWidth);
            nPercentH = ((double)Height / (double)sourceHeight);

            if (nPercentH < nPercentW)
            {
                nPercent = nPercentW;
                switch (Anchor)
                {
                    case AnchorPosition.Top:
                        destY = 0;
                        break;
                    case AnchorPosition.Bottom:
                        destY = (int)(Height - (sourceHeight * nPercent));
                        break;
                    default:
                        destY = (int)((Height - (sourceHeight * nPercent)) / 2);
                        break;
                }
            }
            else
            {
                nPercent = nPercentH;
                switch (Anchor)
                {
                    case AnchorPosition.Left:
                        destX = 0;
                        break;
                    case AnchorPosition.Right:
                        destX = (int)(Width - (sourceWidth * nPercent));
                        break;
                    default:
                        destX = (int)((Width - (sourceWidth * nPercent)) / 2);
                        break;
                }
            }

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

            grPhoto.DrawImage(imgPhoto,
                new Rectangle(destX, destY, destWidth + 2, destHeight + 2),
                new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
                GraphicsUnit.Pixel);

            grPhoto.Dispose();
            return bmPhoto;
        }

        public static Image ConstrainProportions(Image imgPhoto, int Size, Dimensions Dimension)
        {
            int sourceWidth = imgPhoto.Width;
            int sourceHeight = imgPhoto.Height;
            int sourceX = 0;
            int sourceY = 0;
            int destX = -1;
            int destY = -1;
            double nPercent = 0;

            switch (Dimension)
            {
                case Dimensions.Width:
                    nPercent = ((double)Size / (double)sourceWidth);
                    break;
                default:
                    nPercent = ((double)Size / (double)sourceHeight);
                    break;
            }

            int destWidth = (int)((double)sourceWidth * nPercent);
            int destHeight = (int)((double)sourceHeight * nPercent);

            Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

            grPhoto.DrawImage(imgPhoto,
                new Rectangle(destX, destY, destWidth + 2, destHeight + 2),
                new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
                GraphicsUnit.Pixel);

            grPhoto.Dispose();
            return bmPhoto;
        }
    }

    public enum Dimensions
    {
        Width,
        Height
    }

    public enum AnchorPosition
    {
        Top,
        Center,
        Bottom,
        Left,
        Right
    }
}
